from django.db import models

# Create your models here.
from django.db import models
from django.utils import timezone


class QueryLog(models.Model):
    # 字段定义
    instance_name = models.CharField(max_length=100, verbose_name="实例名称")
    database_name = models.CharField(max_length=100, verbose_name="数据库名称")
    query_statement = models.TextField(verbose_name="执行的查询语句")
    returned_rows = models.IntegerField(verbose_name="返回行数")
    execution_time = models.FloatField(verbose_name="执行耗时（秒）")
    operator = models.CharField(max_length=50, verbose_name="操作人")
    operation_time = models.DateTimeField(default=timezone.now, verbose_name="操作时间")

    class Meta:
        verbose_name = "查询日志"
        verbose_name_plural = verbose_name
        ordering = ['-operation_time']

    def __str__(self):
        return f"{self.instance_name} - {self.query_statement[:50]}..."


class AuditLog(models.Model):
    # 字段定义
    user_id = models.IntegerField(verbose_name="用户ID")
    user_name = models.CharField(max_length=100, verbose_name="用户名称")
    action = models.CharField(max_length=20, choices=[('LOGIN', 'Login'), ('LOGOUT', 'Logout')], verbose_name="动作")
    extra_info = models.CharField(max_length=45, blank=True, null=True, verbose_name="额外信息")
    timestamp = models.DateTimeField(default=timezone.now, verbose_name="时间戳")

    class Meta:
        verbose_name = "审计日志"
        verbose_name_plural = verbose_name
        ordering = ['-timestamp']

    def __str__(self):
        return f"{self.user_name} - {self.action} - {self.timestamp}"


class OpLogs(models.Model):
    '''操作日志表'''
    id = models.AutoField(primary_key=True)
    re_time = models.CharField(max_length=32, verbose_name='请求时间')
    re_user = models.CharField(max_length=32, verbose_name='操作人')
    re_ip = models.CharField(max_length=32, verbose_name='请求IP')
    re_url = models.CharField(max_length=255, verbose_name='请求url')
    re_method = models.CharField(max_length=11, verbose_name='请求方法')
    re_content = models.TextField(null=True, verbose_name='请求参数')
    rp_content = models.TextField(null=True, verbose_name='响应参数')
    rp_code = models.TextField(null=True, verbose_name='响应码')
    user_agent = models.TextField(null=True, verbose_name='请求浏览器')
    access_time = models.IntegerField(verbose_name='响应耗时/ms')

    class Meta:
        indexes = [
            models.Index(fields=['re_ip'], ),
            models.Index(fields=['re_url'], ),
            # models.Index(fields=['user_agent'], ),
        ]
        db_table = 'ipam_op_logs'
        verbose_name = '平台操作日志表'
        verbose_name_plural = '平台操作日志表'


class AccessTimeOutLogs(models.Model):
    '''超时操作日志表'''
    id = models.AutoField(primary_key=True)
    re_time = models.CharField(max_length=32, verbose_name='请求时间')
    re_user = models.CharField(max_length=32, verbose_name='操作人')
    re_ip = models.CharField(max_length=32, verbose_name='请求IP')
    re_url = models.CharField(max_length=255, verbose_name='请求url')
    re_method = models.CharField(max_length=11, verbose_name='请求方法')
    re_content = models.TextField(null=True, verbose_name='请求参数')
    rp_content = models.TextField(null=True, verbose_name='响应参数')
    rp_code = models.TextField(null=True, verbose_name='响应码')
    user_agent = models.TextField(null=True, verbose_name='请求浏览器')
    access_time = models.IntegerField(verbose_name='响应耗时/ms')

    class Meta:
        db_table = 'ipam_access_timeout_logs'
        verbose_name = '平台超时操作日志表'
        verbose_name_plural = '平台超时操作日志表'


class LoginLog(models.Model):
    LOGIN_TYPE_CHOICES = ((1, "普通登录"),)
    username = models.CharField(max_length=32, verbose_name="登录用户名", null=True, blank=True, help_text="登录用户名")
    ip = models.CharField(max_length=32, verbose_name="登录ip", null=True, blank=True, help_text="登录ip")
    agent = models.TextField(verbose_name="agent信息", null=True, blank=True, help_text="agent信息")
    browser = models.CharField(max_length=200, verbose_name="浏览器名", null=True, blank=True, help_text="浏览器名")
    os = models.CharField(max_length=200, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    continent = models.CharField(max_length=50, verbose_name="州", null=True, blank=True, help_text="州")
    country = models.CharField(max_length=50, verbose_name="国家", null=True, blank=True, help_text="国家")
    province = models.CharField(max_length=50, verbose_name="省份", null=True, blank=True, help_text="省份")
    city = models.CharField(max_length=50, verbose_name="城市", null=True, blank=True, help_text="城市")
    district = models.CharField(max_length=50, verbose_name="县区", null=True, blank=True, help_text="县区")
    isp = models.CharField(max_length=50, verbose_name="运营商", null=True, blank=True, help_text="运营商")
    area_code = models.CharField(max_length=50, verbose_name="区域代码", null=True, blank=True, help_text="区域代码")
    country_english = models.CharField(max_length=50, verbose_name="英文全称", null=True, blank=True, help_text="英文全称")
    country_code = models.CharField(max_length=50, verbose_name="简称", null=True, blank=True, help_text="简称")
    longitude = models.CharField(max_length=50, verbose_name="经度", null=True, blank=True, help_text="经度")
    latitude = models.CharField(max_length=50, verbose_name="纬度", null=True, blank=True, help_text="纬度")
    login_type = models.IntegerField(default=1, choices=LOGIN_TYPE_CHOICES, verbose_name="登录类型", help_text="登录类型")
    create_datetime = models.DateTimeField(default=timezone.now, verbose_name="时间戳")
    class Meta:
        db_table = "system_login_log"
        verbose_name = "登录日志"
        verbose_name_plural = verbose_name
        ordering = ("-create_datetime",)